package com.ruicar.afs.cloud.afscase.imagemanage.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.autoaudit.service.IRuleAtomDataService;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.feign.ChannelFeignService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.channel.utils.CaseConfig;
import com.ruicar.afs.cloud.afscase.imagemanage.condition.ImageManageCondition;
import com.ruicar.afs.cloud.afscase.imagemanage.service.ImageManageService;
import com.ruicar.afs.cloud.afscase.imagemanage.vo.ApproveImageManageVo;
import com.ruicar.afs.cloud.afscase.imagemanage.vo.ImageManageVo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseChannelInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  add by fangchenliang
 */

@Slf4j
@RestController
@AllArgsConstructor
@Api("案件贷后影像管理")
@RequestMapping("/imageManage")
public class ImageQueryController {
    private ImageManageService imageManageService;
    private IRuleAtomDataService ruleAtomDataService;
    private CaseConfig caseConfig;
    private ChannelFeignService channelFeignService;
    private CaseChannelInfoService caseChannelInfoService;
    private ChannelBaseInfoService channelBaseInfoService;
    private CaseContractInfoService caseContractInfoService;

    @PostMapping(value = "/getImageManageDataList")
    @ApiOperation(value = "多条件分页获取影像管理")
    public IResponse<IPage<ImageManageVo>> getImageManageDataList(@RequestBody ImageManageCondition condition){
        IPage<ImageManageVo> imageManageVoList=imageManageService.getLoanImageManageList(new Page(condition.getPageNumber(),condition.getPageSize()),condition);
        return IResponse.success(imageManageVoList);
    }
    @PostMapping(value = "/getApproveImageManageDataList")
    @ApiOperation(value = "多条件分页获取审批影像管理")
    public IResponse<IPage<ApproveImageManageVo>> getApproveImageManageDataList(@RequestBody ImageManageCondition condition){
        //业务状态（内）
        List statusList = new ArrayList();
        statusList.add(AfsEnumUtil.key(BusinessStateInEnum.APPROVED));
        statusList.add(AfsEnumUtil.key(BusinessStateInEnum.CONDITIONAL_APPROVE));
        statusList.add(AfsEnumUtil.key(BusinessStateInEnum.REJECT));
        statusList.add(AfsEnumUtil.key(BusinessStateInEnum.REPEAL));
        statusList.add(AfsEnumUtil.key(BusinessStateInEnum.CANCEL_ADVICE_CONDITIONAL));
        condition.setBusinessStateInList(statusList);
        IPage<ApproveImageManageVo> approveImageManageVoList=imageManageService.getApproveImageManageList(new Page(condition.getPageNumber(),condition.getPageSize()),condition);
        return IResponse.success(approveImageManageVoList);
    }
    @PostMapping(value = "/getImageRuleParamsByApplyNo")
    @ApiOperation(value = "根据申请编号获取规则参数")
    public IResponse<JSONObject> getImageRuleParamsByApplyNo(@RequestParam("applyNo") String  applyNo){
        return IResponse.success(ruleAtomDataService.getCommonAtomRunParams(applyNo));
    }
    @PostMapping(value = "/getImageRuleParamsByContractNo")
    @ApiOperation(value = "根据合同号获取规则参数")
    public IResponse<JSONObject> getImageRuleParamsByContractNo(@RequestParam("contractNo") String  contractNo){
        return IResponse.success(ruleAtomDataService.getCommonAtomRunParamsLoan(contractNo));
    }

    @PostMapping(value = "/getDealerSignFiles")
    @ApiOperation(value = "根据申请编号获取渠道预留印鉴")
    public IResponse getDealerSignFiles(@RequestParam("contractNo") String  contractNo){
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", caseConfig.getChannelClientId());
        headers.put("clientSecret", caseConfig.getChannelClientSecret());
        CaseContractInfo caseContractInfo=caseContractInfoService.getOne(Wrappers.<CaseContractInfo>lambdaQuery().eq(CaseContractInfo::getContractNo,contractNo));
        CaseChannelInfo caseChannelInfo=caseChannelInfoService.getOne(Wrappers.<CaseChannelInfo>lambdaQuery().eq(CaseChannelInfo::getApplyNo,caseContractInfo.getApplyNo()));
        ChannelBaseInfo channelBaseInfo=channelBaseInfoService.getOne(Wrappers.<ChannelBaseInfo>lambdaQuery().eq(ChannelBaseInfo::getChannelCode,caseChannelInfo.getDealerNo()));
        return channelFeignService.getChannelSeal(channelBaseInfo.getId().toString(), headers);
    }
}
